/**
 * 
 */
package com.rangers.auth.sample.jpa.ctrl;

import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.rangers.auth.base.details.AuthUser;
import com.rangers.auth.base.service.UserService;
import com.rangers.auth.sample.jpa.entity.Scope;
import com.rangers.auth.sample.jpa.entity.User;
import com.rangers.auth.sample.jpa.repo.ScopeRepo;
import com.rangers.auth.sample.jpa.repo.UserRepo;

/**
 * 系统初始化数据
 * @version 1.0
 * @author fuhw
 * @date 2017年8月22日 下午7:09:23
 */
@Component
public class SysDataInit implements InitializingBean {

	private final UserService<User, AuthUser, UserRepo, String> userService;
	private final ScopeRepo scopeRepo;
	@Autowired
	public SysDataInit(UserService<User, AuthUser, UserRepo, String> userService,ScopeRepo scopeRepo) {
		super();
		this.userService = userService;
		this.scopeRepo = scopeRepo;
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		List<User> list = userService.list();
		if(list == null || list.isEmpty()) {
			User user = new User();
			user.setUsername("admin");
			user.setPassword("admin");
			Scope scope = new Scope();
			scope.setName("浦东新区");
			scopeRepo.save(scope);
			user.setScopeId(scope.getId());
			userService.create(user);
		}
	}
}
